﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
	public static partial class msg
	{
		public static dynamic getDiscussList(int start, int limit, bool god)
		{
			var sql = new Sql.SqlDataContext();
			var total = (from d in sql.Discuss
						 from p in sql.Problem
						 where !d.parentDid.HasValue && p.pid == d.pid && (god || !p.disabled)
						 select new { }).Count();
			var result = (from d in sql.Discuss
						  from u in sql.User
						  from p in sql.Problem
						  where !d.parentDid.HasValue && u.username == d.username && p.pid == d.pid && (god || !p.disabled)
						  orderby d.did descending
						  select new {
							  id = d.did,
							  d.did,
							  d.parentDid,
							  d.title,
							  d.len,
							  u.nickname,
							  u.username,
							  time = d.time.toTimeString(),
							  d.pid,
							  problemTitle = p.title,
							  leaf = !sql.Discuss.Any(row => row.parentDid == d.did)
						  }).Skip(start).Take(limit);
			return new {
				success = true,
				total,
				result
			};
		}
	}
}
